package com.example.mideaoem.data;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import com.example.mideaoem.data.status.IDataBodyDevStauts;
import com.example.mideaoem.utils.TemperatureUtil;

import android.util.Log;

public class DataResponseOld extends FactoryDataBody{

	/**
	 * 第零个byte
	 */
	byte Zerobyte;
	
	/**
	 * 第一个byte
	 */
	byte FirstByte;
	
	byte powerStatus;		//开机状态
	byte imodeResume;		//i模式恢复
	byte childSleepMode;		//儿童睡眠模式
	byte timerMode;				//定时模式
	byte test2;				//快检
	byte errMark;			//故障标志
	
	/**
	 * 第二个byte
	 */
	byte Secondbyte;
	int setTemperature;			//设定温度
	byte setTemperature_dot;		//设定温度-小数 0.5
	byte mode;					//模式  自动-0x20 制冷-0x40 抽湿-0x60 制热-0x80 送风-0xa0
	
	/**
	 * 第三个byte
	 */
	byte Thirdbyte;
	byte fanSpeed;			//风速  固定风-0x65
	boolean timerEffe;			//移动终端定时有效
	
	/**
	 * 第四个byte
	 */
	byte Fourthbyte;
	byte timer_on_hour;		//定时开-小时
	byte timer_on_in_min;	//定时开-单位15分
	byte timer_on;			//定时开功能
	
	/**
	 * 第五个byte
	 */
	byte Fifthbyte;
	byte timer_off_hour;	//定时关-小时
	byte timer_off_in_min;	//定时关-单位15分
	byte timer_off;			//定时关功能
	
	/**
	 * 第六个byte
	 */
	byte Sixthbyte;
	byte timer_off_min;		//定时关-单位分
	byte timer_on_min;		//定时开-单位分
	
	/**
	 * 第七个byte  ？
	 */
	byte Seventhbyte;
	
	/**
	 * 第八个byte
	 * 舒睡 强劲 睿风
	 */
	byte Eighthbyte;
	byte cosySleep;			//舒睡	00-无舒睡 01-舒睡1 02-舒睡2 03-舒睡3 
	byte save;				//省电	
	byte lowFerqFan;		//睿风	
	byte superFan;			//强劲	
	byte feelOwn;		//随身感
	
	
	/**
	 * 第九个byte
	 * 自然风 干燥 电辅热 ECO 随身感 本机熟睡
	 */
	byte Ninethbyte;
	byte childSleep;		//儿童舒睡模式
	byte naturalFan;		//自然风
	byte dryClean;			//干燥
	byte ptcAssis;			//电辅热
	byte eco;				//ECO
	byte cleanUp;			//净化
	byte slefCosySleep;		//本机舒睡
	byte selfFeelOwn;			//本机随身感
	
	/**
	 * 第十个byte
	 * 换气 小夜灯 防着凉 峰谷省电
	 */
	byte Tenthbyte;
	byte sleepFunc;			//Sleep功能状态
	byte tubro;				//TUBRO涡轮
	byte tempUnit;			//温度单位
	byte exchangeAir;		//换气
	byte nightLight;		//小夜灯
	byte catchCold;			//防着凉
	byte peakElec;			//峰谷节电
	byte coolFan;			//凉风模式
	
	/**
	 * 第十一个byte
	 * 室内温度
	 */
	byte Eleventhbyte;
	double indoor_temp;		//室内温度 整数部分
	
	/**
	 * 第十二个byte
	 */
	byte Twelfthbyte;
	double outdoor_temp;		//室外温度 整数部分
	
	/**
	 * 第十三个byte
	 */
	byte Thirteenthbyte;
	int expand_temp;		//扩展温度
	byte dusFull;			//尘满
	
	/**
	 * 第十四个byte
	 */
	byte Fourteenthbyte;
	byte pmvMode;			//PWV模式
	byte light;				//亮度
	
	/**
	 * 第十五个byte
	 */
	byte Fifteenthbyte;
	byte T1_dot;			//T1温度小数
	byte T4_dot;			//T4温度小数
	
	/**
	 * 第十六个byte
	 * 故障代码
	 */
	byte Sixteenthbyte;
	
	
	/**
	 * 第十七个byte
	 */
	byte Seventeenthbyte;
	
	/**
	 * 第十八个byte
	 */
	byte Eighteenthbyte;
	
	/**
	 * 第19byte
	 */
	byte Nineteenthbyte;
	
	/**
	 * 第20byte
	 */
	byte Twentieth;
	
	/**
	 * 第21byte
	 */
	byte Twenty_first;
	byte setExpand_dot;			//扩展温度-小数
	byte setExpand;				//扩展温度
	byte double_temp;			//双温控制
	byte Eight_Hot;				//8度制热
	
	@Override
	public byte[] toBytes() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object toObject(byte[] bytes) {
		// TODO Auto-generated method stub
		DeviceStatus status = new DeviceStatus();
		if(bytes == null){
			return new DeviceStatus();
		}
		
		Zerobyte = bytes[0];
		FirstByte = bytes[1];
		status.powerStatus = powerStatus = (byte)(bytes[1] & 0x01);
		status.imodeResume = imodeResume = (byte) ((bytes[1] & 0x04) >> 2);
		status.timerMode = timerMode = (byte) ((bytes[1] & 0x10) >> 1);
		status.test2 = test2 = (byte) ((bytes[1] & 0x20) >> 5);
		status.errMark = errMark = (byte) ((bytes[1] & 0x80) >> 7);
		
		Secondbyte = bytes[2];
		status.setTemperature = setTemperature = (int)((bytes[2] & 0x0f)+16);
		status.setTemperature_dot = setTemperature_dot = (byte) ((bytes[2] & 0x10) >> 1);
		status.mode = mode = (byte) ((bytes[2] & 0xE0) >> 5);
		
		Thirdbyte = bytes[3];
		status.fanSpeed = fanSpeed = (byte) (bytes[3] & 0x7f);
		
		if(fanSpeed < IDataBodyDevStauts.WIND_SPEED_RANGE_LOW){
			status.fanSpeed = IDataBodyDevStauts.WIND_SPEED_LOW;
		}else if(fanSpeed < IDataBodyDevStauts.WIND_SPEED_RANGE_MIDDLE){
			status.fanSpeed = IDataBodyDevStauts.WIND_SPEED_MIDDLE;
		}else if(fanSpeed < IDataBodyDevStauts.WIND_SPEED_RANGE_HIGH){
			status.fanSpeed = IDataBodyDevStauts.WIND_SPEED_HIGH;
		}else if(fanSpeed == IDataBodyDevStauts.WIND_SPEED_FIXED || fanSpeed == IDataBodyDevStauts.WIND_SPEED_AUTO){
			status.fanSpeed = IDataBodyDevStauts.WIND_SPEED_AUTO;
		}
			
		Fourthbyte = bytes[4];
		
		status.timer_on_hour = timer_on_hour = (byte) ((bytes[4] & 0x7c) >> 2);
		status.timer_on_min = (byte) ((bytes[4] & 0x03) | ((bytes[6] & 0xf0) >> 4));
		status.timer_on = timer_on = (byte) ((bytes[4] & 0x80) >> 7);
		
		Fifthbyte = bytes[5];
		status.timer_off_hour = timer_off_hour = (byte) ((bytes[5] & 0x7c) >> 2);
		status.timer_off_min = (byte) ((bytes[5] & 0x03) | (bytes[6] & 0x0f));
		status.timer_off = timer_off = (byte)((bytes[5] & 0x80)>>7);
		
		if(timer_off  == 0){
			status.timer_off_hour = status.timer_off_min = 0;
		}
		if(timer_on == 0){
			status.timer_on_hour = status.timer_on_min = 0;
		}
		
		Seventhbyte = bytes[7];
		status.updownFan = ((bytes[7] & 0x0c) != 0) ? (byte)0x01:(byte)0x00;
		status.leftRightFan = ((bytes[7] & 0x03) != 0) ? (byte) 0x01 : (byte) 0x00;
		
		Eighthbyte = bytes[8];
		status.cosySleep = cosySleep = (byte) (bytes[8] & 0x03);
		status.save = save = (byte) ((bytes[8] & 0x08)>>3);
 		status.lowFerqFan = lowFerqFan = (byte) ((bytes[8] & 0x10)>>4);
		status.tubro = superFan = (byte) ((bytes[8] & 0x20)>>5);
		status.feelOwn = feelOwn = (byte)((bytes[8]& (byte)0x80) >> 7);
		
		Ninethbyte = bytes[9];
		status.childSleepMode = childSleep = (byte)(bytes[9]& (byte)0x01);
		status.naturalFan = naturalFan = (byte)((bytes[9]& (byte)0x02)>>1);
		status.dryClean = dryClean = (byte)((bytes[9]&(byte)0x04)>>2);
		status.ptcAssis = ptcAssis = (byte)((bytes[9]& (byte)0x08)>>3);
		status.eco = eco = (byte)((bytes[9]& (byte)0x10)>>4);
		status.cleanUp = cleanUp = (byte)((bytes[9]& (byte)0x20)>>5);
		status.sleepFunc = slefCosySleep = (byte)((bytes[9]& (byte)0x40)>>6);
		status.selfFeelOwn = selfFeelOwn = (byte)((bytes[9]& (byte)0x80)>>7);
		
		Tenthbyte = bytes[10];
		status.sleepFunc = sleepFunc = (byte)(bytes[10]& (byte)0x01);
		if(status.tubro == 0){
			status.tubro = tubro = (byte)((bytes[10]& (byte)0x02)>>1);
		}
		status.tempUnit = tempUnit = (byte)((bytes[10]& (byte)0x04)>>2);
		status.exchangeAir = exchangeAir = (byte)((bytes[10]& (byte)0x08)>>3);
		status.nightLight = nightLight = (byte)((bytes[10]& (byte)0x10)>>4);
		status.catchCold = catchCold = (byte)((bytes[10]& (byte)0x20)>>5);
		
		Log.d("catchCold", "收到  "+status.catchCold);
		
		status.peakElec = peakElec = (byte)((bytes[10]& (byte)0x40)>>6);
		status.coolFan = coolFan = (byte)((bytes[10]& (byte)0x80)>>7);
		
		Eleventhbyte = bytes[11];
		status.indoor_temp = 255;
		try{
			Log.d("rawData", "byte[11] = "+(int)(bytes[11]&0xff));
			indoor_temp = ((int)(bytes[11]&0xff) - 50)/2.0;
			status.indoor_temp = (int)indoor_temp;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		Twelfthbyte = bytes[12];
		status.outdoor_temp = 255;
		try{
			if(bytes[12] < 0){
				bytes[12] += 256;
			}
			Log.d("rawData","byte[12] = "+(int)(bytes[12]&0xff));
			outdoor_temp = ((int)(bytes[12]&0xff) - 50)/2.0;
			status.outdoor_temp = (int)outdoor_temp;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		Thirteenthbyte = bytes[13];
//		status.expand_temp = expand_temp = (int)(bytes[13] & 0x1F);
		status.dusFull = dusFull = (byte)((bytes[13] & 0x20) >> 5);
		
		Fourteenthbyte = bytes[14];
		status.pwmMode = pmvMode = (byte) (bytes[14] & 0x0F);
		status.light = light = (byte) ((bytes[14] & 0x70)>>4);
		
		Fifteenthbyte = bytes[15];
		status.T1_dot = T1_dot = (byte) (bytes[15] & 0x0F);
		status.T4_dot = T4_dot = (byte) ((bytes[15] & 0xF0) >> 4);
		
		Sixteenthbyte = bytes[16];
		status.errInfo = bytes[16];
		
//		Seventeenthbyte = bytes[17];
		
//		Eighteenthbyte = bytes[18];
		
//		Nineteenthbyte = bytes[19];
		
		if(bytes.length == 22){
			status.double_temp = 0;
			status.Eight_Hot = 0;
		}else if(bytes.length == 24){
			try{
				Twentieth = bytes[20];
				Twenty_first = bytes[21];
				status.double_temp = double_temp = (byte)((bytes[21]& (byte) 0x40)>>6);
				status.Eight_Hot = Eight_Hot = (byte) (((bytes[21] & (byte) 0x80) == 0) ? (byte) 0x00 : 0x01);
			}catch(Exception e){
				e.printStackTrace();
				status.double_temp = 0;
				status.Eight_Hot = 0;
			}
		}else{
			status.double_temp = 0;
			status.Eight_Hot = 0;
		}
		
		Log.d("rawData", "indoor  = "+status.indoor_temp);
		
		if (tempUnit == IDataBodyDevStauts.TEMPERATURE_Fahrenheit) {
			// 华氏度温度转换
			int dot = 0;
			if (status.setTemperature_dot != 0) {
				dot = 5;
			}
			DecimalFormat digits = (DecimalFormat)NumberFormat.getInstance(Locale.ENGLISH);
			digits.applyPattern("0.0");
			if (inRange(indoor_temp)) {
				status.indoor_temp = Integer.valueOf(TemperatureUtil.centigrade2Fahrenheit(""+digits.format(indoor_temp)));
			} else {
				status.indoor_temp = (int) (indoor_temp * 1.8 + 32);
			}
			if (inRange(outdoor_temp)) {
				status.outdoor_temp = Integer
						.valueOf(TemperatureUtil.centigrade2Fahrenheit(""+digits.format(outdoor_temp)));
			} else {
				status.outdoor_temp = (int) (outdoor_temp * 1.8 + 32);
			}
			
			status.setTemperature = Integer
					.valueOf(TemperatureUtil.centigrade2Fahrenheit(setTemperature + "." + dot));
		}
		
		return status;
	}

	private boolean inRange(double num) {
		if(num <= 17.0 || num >= 30.0){
			return false;
		}else{
			return true;
		}
	}

	private byte getTargetBit(byte b, byte target){
		int index = 0;
		if(target == 0x02){
			index = 1;
		}else if(target == 0x04){
			index = 2;
		}else if(target == 0x08){
			index = 3;
		}
		else if(target == 0x10){
			index = 4;
		}
		else if(target == 0x20){
			index = 5;
		}
		else if(target == 0x40){
			index = 6;
		}else if(target == 0x80){
			index = 7;
		}
		return (byte) ((b & target) >> index);
	}

	@Override
	public void setDataBodyStatus(BaseDevice newStatus) {
		// TODO Auto-generated method stub
		
	}	
}
